﻿using AutoMapper;
using CSRedis;
using IntelligentAgriculture.Contracts.Dto.Input;
using IntelligentAgriculture.Contracts.Dto.Output;
using IntelligentAgriculture.Contracts.Interface;
using IntelligentAgriculture.Domain.Entity;
using IntelligentAgriculture.Domain.Interface;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yitter.IdGenerator;

namespace IntelligentAgriculture.Services
{
    /// <summary>
    /// 羊场服务实现
    /// </summary>
    public class SheepService:ISheepService
    {

        private readonly ISheepRepository sheepRe;
        private readonly ILogger<SheepService> logger;
        private readonly IMapper imapper;
        public CSRedisClient _csreCilent;
        public SheepService(ISheepRepository sheepRe, ILogger<SheepService> logger, IMapper imapper)
        {
            this.sheepRe = sheepRe;
            this.logger = logger;
            this.imapper = imapper;
            _csreCilent = new CSRedisClient("127.0.0.1:6379");
        }
        
        /// <summary>
        /// 羊场列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PageOutput<SheepOutPut>> SheepFactoryList(SheepInPut input)
        {

            try
            {    logger.LogInformation("羊场列表");
                //var list =/* sheepRe.SheepFactory().Where(c => c.Isdelete == 0);*/
                var list= _csreCilent.Get<List<Sheepfactory>>("SheepfactoryList");
                if (list == null || list.Count == 0)
                {
                    list =await sheepRe.SheepFactory();
                    _csreCilent.Set("SheepfactoryList", list);
                }

                if (!string.IsNullOrEmpty(input.SheepFullName))
                {
                    list = list.Where(c => c.SheepFullName.Contains(input.SheepFullName)).ToList();
                }
                if (!string.IsNullOrEmpty(input.ResponsiblePerson))
                {
                    list = list.Where(c => c.ResponsiblePerson.Contains(input.ResponsiblePerson)).ToList();
                }
                var result = new PageOutput<SheepOutPut>
                {
                    TotalCount = list.Count(),
                    PageSize = input.PageSize,
                };

                list = list.OrderBy(c => c.SheepId).Skip((input.PageIndex - 1) * input.PageSize).Take(input.PageSize).ToList();
                result.Data = imapper.Map<List<SheepOutPut>>(list);
                return result;
            }
            catch (Exception ex)
            {
                logger.LogError(ex, ex.Message);
                throw;
            }
        }

      
        /// <summary>
        /// 羊场添加
        /// </summary>
        /// <param name="sheep"></param>
        /// <returns></returns>
        public async Task<int> Add(Sheepfactory sheep)
        {
            try
            {   logger.LogInformation("添加羊场");
                sheep.EstablishingTime = DateTime.Now;
            
                sheep.SheepNumber = YitIdHelper.NextId().ToString();
                _csreCilent.Del("SheepfactoryList");
                var entity = imapper.Map<Sheepfactory>(sheep);
                return await sheepRe.Add(entity);
            }
            catch (Exception ex)
            {
                logger.LogError(ex, ex.Message);
                throw;
            }
           
        }


        /// <summary>
        /// 羊场删除
        /// </summary>
        /// <param name="sheepId"></param>
        /// <param name="idList"></param>
        /// <returns></returns>
        public async Task<int> Delete(int sheepId, params int[] idList)
        {
            try 
            {  
                logger.LogInformation("删除羊场");
                _csreCilent.Del("SheepfactoryList");
                return await sheepRe.Delete(sheepId, idList);
            }
            catch (Exception ex)
            {
                logger.LogError(ex, ex.Message);
                throw;
            }
          
        }

        /// <summary>
        /// 羊场修改
        /// </summary>
        /// <param name="sheep"></param>
        /// <returns></returns>
        public async Task<int> Update(Sheepfactory sheep)
        {
            try
            {   logger.LogInformation("更新羊场");
                _csreCilent.Del("SheepfactoryList");
                var entity = imapper.Map<Sheepfactory>(sheep);
                return await sheepRe.Update(entity);
            }
            catch (Exception ex)
            {
                logger.LogError(ex, ex.Message);
                throw;
            }
        
        }


        /// <summary>
        /// 羊场反填
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<SheepOutPut> GetSheep(SheepGetInPut input)
        {
            try
            {   logger.LogInformation("获取羊场");
                return imapper.Map<SheepOutPut>(sheepRe.GetSheep(input.SheepId));
            }
            catch (Exception ex)
            {
                logger.LogError(ex, ex.Message);
                throw;
            }
            
        }



    }





}
